<?php

namespace plugin\admin\app\controller\image;


use plugin\admin\app\logic\font\FontLogic;
use plugin\admin\app\logic\image\AiimagecommandLogic;
use support\Request;
use support\Response;
use plugin\admin\app\controller\AuthController;
use support\exception\BusinessException;

class AiimagecommandController extends AuthController
{
    
    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {

        return view('image/aiimagecommand/index');
    }

    /**
     * 查询
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function select(Request $request): Response
    {
        return AiimagecommandLogic::getInstance()->select($request);
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return AiimagecommandLogic::getInstance()->insert($request);
        }
        $aiImageCommand = json_decode(getAiCommand('ai_image_command_template'),true);
        $aiImageCommand = $aiImageCommand ? json_encode($aiImageCommand,JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) : '';

        $aiCommandSamplers = array_filter(explode("|@@|",config('param.ai_command_sampler_list','')));
        $aiCommandScheduler = array_filter(explode("|@@|",config('param.ai_command_scheduler_list','')));
        return view('image/aiimagecommand/insert',[
            'ai_image_command'=>$aiImageCommand,
            'ai_command_sampler_list'=>json_encode($aiCommandSamplers,256),
            'ai_command_scheduler_list'=>json_encode($aiCommandScheduler,256)
        ]);
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
    */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return AiimagecommandLogic::getInstance()->update($request);
        }

        $aiCommandSamplers = array_filter(explode("|@@|",config('param.ai_command_sampler_list','')));
        $aiCommandScheduler = array_filter(explode("|@@|",config('param.ai_command_scheduler_list','')));
        return view('image/aiimagecommand/update',[
                'ai_command_sampler_list'=>json_encode($aiCommandSamplers,256),
                'ai_command_scheduler_list'=>json_encode($aiCommandScheduler,256)
        ]);
    }

    /**
     * 删除
     * @param Request $request
     * @return Response
     * @throws \support\exception\BusinessException
     */
    public function delete(Request $request): Response
    {
        return AiimagecommandLogic::getInstance()->delete($request);
    }

    /**
     * 预览指令
     * @param Request $request
     * @return Response
     * @throws \support\exception\BusinessException
     */
    public function viewCommand(Request $request): Response
    {
        $aiImageCommand = json_decode(getAiCommand('ai_image_command_template'),true);
        if($aiImageCommand){
            $aiImageCommand['prompt'] = $request->input('prompt','');
            $aiImageCommand['negative_prompt'] = $request->input('negative_prompt','');
            $aiImageCommand['sampler_name'] = $request->input('sampler','');
            $aiImageCommand['sampler_index'] = $request->input('sampler','');
            $aiImageCommand['scheduler'] = $request->input('scheduler','');
            $aiImageCommand['steps'] = (int) $request->input('steps','');
            $aiImageCommand['override_settings']['sd_model_checkpoint'] = $request->input('model','');
            $aiImageCommand['override_settings']['sd_vae'] = $request->input('sd_vae','');
            if(!empty($request->input('adetailer'))){
                $aiImageCommand['alwayson_scripts']['ADetailer']['args'] = [
                    [
                        'ad_model'=>'ace_yolov8n.pt',
                        'ad_prompt'=>'',
                        'ad_confidence'=>'0.7',
                        'ad_denoising_strength'=>'0.7',
                   ]
                ];
            }else{
                if(isset($aiImageCommand['alwayson_scripts']) && isset($aiImageCommand['alwayson_scripts']['ADetailer'])){
                    unset($aiImageCommand['alwayson_scripts']);
                }
            }
        }
        $aiImageCommand = $aiImageCommand ? json_encode($aiImageCommand,JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) : '';

        return json([0,'ok','data'=>['ai_image_command'=>$aiImageCommand]]);
    }

    /**
     * 复制
     * @param Request $request
     * @return Response
     * @throws \support\exception\BusinessException
     */
    public function copy(Request $request): Response
    {
        return AiimagecommandLogic::getInstance()->copy($request);
    }
}
